package com.yin.myproject.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @author Eason
 * @create 2017-12-24 18:10
 **/
public class GenericClient {

    private ChildClientHandler clientHandler;

    public GenericClient(ChildClientHandler clientHandler) {
        this.clientHandler = clientHandler;
    }

    public void connect(int port, String host) throws Exception{
        EventLoopGroup group = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(group)
                .channel(NioSocketChannel.class)
                .option(ChannelOption.TCP_NODELAY,true)
                .handler(clientHandler);
        ChannelFuture f = bootstrap.connect(host,port).sync();
        f.channel().closeFuture().sync();
    }
}
